package com.qiaofei.controller.api;

import com.qiaofei.pojo.Estate;
import com.qiaofei.pojo.PageBean;
import com.qiaofei.pojo.Result;
import com.qiaofei.service.impl.EstateServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 小区信息管理Controller
 */
@Slf4j
@RestController
@RequestMapping("/api/estates")
public class EstateController extends BaseController{

    @Autowired
    private EstateServiceImpl estateService;

    @GetMapping
    public Result page(@RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer pageSize){
        log.info("分页查询, 参数: {},{},{},{},{},{}",page,pageSize);
        //调用service分页查询
        PageBean pageBean = estateService.page(page,pageSize);
        return Result.success(pageBean);
    }

    @GetMapping("/findAll")
    public Result list(){
        log.info("查询全部数据");
        //调用service查询数据
        List<Estate> estateList =  estateService.list();
        return Result.success(estateList);
    }

    /**
     * 新增
     * @return
     */
    @PostMapping
    public Result add(@RequestBody Estate estate){
        log.info("新增: {}" , estate);
        //调用service新增
        estateService.add(estate);
        return Result.success();
    }

    /**
     * 编辑
     * @param estate
     * @return
     */
    @PutMapping
    public Result update(@RequestBody Estate estate){
        log.info("编辑信息: {}" , estate);
        //调用service新增管理员
        estateService.update(estate);
        return Result.success();
    }

    /**
     * id查询
     * @param ids
     * @return
     */
    @GetMapping("/{ids}")
    public Result getByIds(@PathVariable List<Integer> ids){
        log.info("id查询, id:{}",ids);
        List<Estate> estateList = estateService.getByIds(ids);
        return Result.success(estateList);
    }

    @DeleteMapping("/{ids}")
    public Result delete(@PathVariable List<Integer> ids){
        log.info("批量删除操作, ids:{}",ids);
        estateService.delete(ids);
        return Result.success();
    }
}
